﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description:  ViewDefOptionValueViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    /// <summary>
    /// Viewdef Option param type
    /// </summary>
    public class ViewDefOptionValueViewModel : ViewModelBase, IDataErrorInfo, ICleanupParamsAndPayload
    {
        private readonly OptionValue optionValue;
        private ViewDefParameterLocalizablePropertyViewModel val;
        private readonly ViewDefViewModel viewDefViewModel;

        public ViewDefOptionValueViewModel(OptionValue optionValue, ViewDefViewModel viewDefViewModel)
        {
            Debug.Assert(optionValue != null && viewDefViewModel != null);
            this.optionValue = optionValue;        
            this.viewDefViewModel = viewDefViewModel;
        }

        public static class PropertyNames
        {
            public const string Key = "Key";
            public const string OptionValue = "OptionValue";
        } 

        public OptionValue Option
        {
            get { return this.optionValue; }
        }

        public string Key
        {
            get
            {
                if (string.IsNullOrEmpty(optionValue.Key))
                {
                    return "Enter a new option (key)";
                }
                return optionValue.Key;
            }
            set
            {
                if (value != null)
                {
                    optionValue.Key = value;
                }               
                this.OnPropertyChanged(PropertyNames.Key);
            }
        }

        public ViewDefParameterLocalizablePropertyViewModel OptionValue
        {
            get
            {
                if (this.val == null)
                {
                    Func<string> paramValueGetter = () => this.optionValue.Value ?? "Enter a option (value)";
                    Action<string> paramValueSetter = delegate(string value)
                        {
                            this.optionValue.Value = value;                            
                        };

                    Func<string> autoParamNameGetter = () =>
                        {
                            Random rand = new Random();
                            if (this.optionValue.Key != null)
                            {
                                return PropertyNames.OptionValue + this.optionValue.Key;
                            }
                            return PropertyNames.OptionValue + "_" + rand.Next().ToString(CultureInfo.InvariantCulture);
                        };

                    Func<string> defaultLocalizedValue = () => this.optionValue.Key ?? string.Empty;                   

                    this.val = new ViewDefParameterLocalizablePropertyViewModel(
                        PropertyNames.OptionValue,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamNameGetter,
                        this.viewDefViewModel,
                        true,
                        defaultLocalizedValue);
                }
                return this.val;
            }
        }


        public string this[string propertyName]
        {
            get
            {
                switch (propertyName)
                {
                    case PropertyNames.Key:
                        if (string.IsNullOrEmpty(this.optionValue.Key))
                        {
                            return "Option Key cannot be empty";
                        }                       
                        break;
                    case PropertyNames.OptionValue:
                        if (string.IsNullOrEmpty(this.optionValue.Key))
                        {
                            return "Option Value cannot be empty";
                        }
                        break;
                }
                return null;
            }
        }

        public string Error { get; set; }


        public void Cleanup()
        {
            if (this.val != null)
            {
                this.val.Cleanup();
            }           
        }
    }
}
